package com.wentao.thisService.controller;

import com.wentao.thisCommon.model.RequestBase;
import com.wentao.thisCommon.model.ResultBase;
import com.wentao.thisFacade.business.AppCtrInterface;
import com.wentao.thisFacade.common.UserInfo;
import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.SuccessCode;
import com.wentao.thisFacade.domain.reqeust.*;
import com.wentao.thisFacade.domain.result.*;
import com.wentao.thisService.common.TokenVerify;
import com.wentao.thisService.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class AppController implements AppCtrInterface {

    @Autowired
    private CommunalService communalService;

    @Autowired
    private VersionService versionService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CacheLocationService cacheLocationService;
    @Autowired
    private CacheUserService cacheUserService;

    @Override
    public ResultBase<List<LocationProvinceResult>> test1() {
        Map<String, LocationProvinceResult> allLocationProvinceCache = cacheLocationService.getAllLocationProvinceCache();
        List<LocationProvinceResult> resultList = new ArrayList<>(allLocationProvinceCache.values());
        return ResultBase.ok(resultList,SuccessCode.SYSTEM_SUCCESS);
    }

    @Override
    public ResultBase<List<UserInfo>> test2() {
        Map<String, UserInfo> allUserInfoCache = cacheUserService.getAllUserInfoCache();
        List<UserInfo> resultList = new ArrayList<>(allUserInfoCache.values());
        return ResultBase.ok(resultList,SuccessCode.SYSTEM_SUCCESS);
    }

    /**
     * @see AppCtrInterface#checkVersion(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    public ResultBase<VersionCheckResult> checkVersion(RequestBase<VersionCheckRequest> entity) {
        try {
            VersionCheckResult versionCheckResult = versionService.checkVersion(entity.getBody());
            return ResultBase.ok(versionCheckResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#hotelRegister(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    public ResultBase<PhoneBackUtilsResult> hotelRegister(RequestBase<HotelRegisterRequest> entity) {
        try {
            PhoneBackUtilsResult phoneBackUtilsResult = companyService.hotelRegister(entity.getBody());
            return ResultBase.ok(phoneBackUtilsResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#addCompanyCustom(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<Boolean> addCompanyCustom(RequestBase<AddCompanyCustom> entity) {
        try {
            Boolean flag = companyService.addCompanyCustom(entity.getBody());
            return ResultBase.ok(flag, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#operatorInformationEntry(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<PhoneBackUtilsResult> operatorInformationEntry(RequestBase<OperatorListRequest> entity) {
        try {
            Boolean flag = companyService.operatorInformationEntry(entity.getBody());
            return ResultBase.ok(flag, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#signContract(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<SignContractResult> signContract(RequestBase<SignContractRequest> entity) {
        try {
            SignContractResult signContractResult = companyService.signContract(entity.getBody().getCompanyCode());
            return ResultBase.ok(signContractResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#hotelReviewStatus(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<CompanyAddressInfoResult> hotelReviewStatus(RequestBase<HotelDetailsRequest> entity) {
        try {
            CompanyAddressInfoResult companyAddressInfoResult = communalService.hotelReviewStatus(entity.getBody().getCompanyCode());
            return ResultBase.ok(companyAddressInfoResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#hotelDetails(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<CompanyCustomResult> hotelDetails(RequestBase<HotelDetailsRequest> entity) {
        try {
            CompanyCustomResult companyCustomResult = companyService.hotelDetails(entity.getBody().getCompanyCode());
            return ResultBase.ok(companyCustomResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#goodsList(RequestBase)
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    @TokenVerify
    public ResultBase<List<GoodsInfoResult>> goodsList(RequestBase entity) {
        try {
            List<GoodsInfoResult> goodsInfoResultList = goodsService.goodsList();
            return ResultBase.ok(goodsInfoResultList, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#goodsPropertyInfo(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<GoodsInfoResult> goodsPropertyInfo(RequestBase<GoodsPropertyInfoRequest> entity) {
        try {
            GoodsInfoResult goodsInfoResult = goodsService.goodsPropertyInfo(entity.getBody().getGoodsId());
            return ResultBase.ok(goodsInfoResult, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#createOrderInfo(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<OrderInfoResult> createOrderInfo(RequestBase<CreateOrderInfoListRequest> entity) {
        try {
            OrderInfoResult orderInfo = orderService.createOrderInfo(entity.getBody(), Long.valueOf(entity.getUserId()));
            return ResultBase.ok(orderInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#orderList(RequestBase)
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    @TokenVerify
    public ResultBase<List<OrderInfoResult>> orderList(RequestBase entity) {
        try {
            List<OrderInfoResult> orderInfoList = orderService.listOrderByUserId(Long.valueOf(entity.getUserId()));
            return ResultBase.ok(orderInfoList, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#orderInfo(RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<OrderInfoResult> orderInfo(RequestBase<SearchOrderInfoRequest> entity) {
        try {
            OrderInfoResult orderInfo = orderService.orderInfo(Long.valueOf(entity.getBody().getOrderId()));
            return ResultBase.ok(orderInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#orderConfirmReceived(RequestBase) (RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<OrderInfoResult> orderConfirmReceived(RequestBase<OrderPayRequest> entity) {
        try {
            OrderInfoResult orderInfo = orderService.orderConfirmReceived(entity.getBody().getOrderId());
            return ResultBase.ok(orderInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#changeOrderPayStatus(RequestBase) (RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
    @TokenVerify
    public ResultBase<OrderInfoResult> changeOrderPayStatus(RequestBase<OrderPayRequest> entity) {
        try {
            OrderInfoResult orderInfo = orderService.changeOrderPayStatus(entity.getBody());
            return ResultBase.ok(orderInfo, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }

    /**
     * @see AppCtrInterface#searchSettingList(RequestBase) (RequestBase)
     */
    @SuppressWarnings("unchecked")
    @Override
//    @MethodAuthority(permissionCode = {"ADMIN_ALLOW_FUNC"})
    public ResultBase<List<SettingMapResult>> searchSettingList(@RequestBody RequestBase<SearchSettingListRequest> entity) {
        try {
            List<SettingMapResult> settingMapResultList = communalService.searchSettingList(entity);
            return ResultBase.ok(settingMapResultList, SuccessCode.SYSTEM_SUCCESS);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultBase.ok(ExceptionCode.ON_FAILURE);
        }
    }
}
